package com.brave.user.controller;

import com.brave.base.BaseController;
import com.brave.base.CheckNullParamKit;
import com.brave.base.MobileKit;
import com.brave.base.RedisKit;
import com.brave.config.AliSMSConfiguration;
import com.brave.user.dto.FindOutPasswordDTO;
import com.brave.user.dto.RedisUserInfo;
import com.brave.user.dto.RegisterDTO;
import com.brave.user.dto.UpdateUserInfoDTO;
import com.brave.user.entity.MCUserBaseInfo;
import com.brave.user.entity.MCUserBaseInfoImp;
import com.brave.user.entity.MCUserInviteMappingEntity;
import com.brave.user.repository.MCUserBaseInfoImpRepository;
import com.brave.user.repository.MCUserBaseInfoRepository;
import com.brave.user.repository.MCUserInviteMappingRepository;
import com.brave.user.service.UserUpdateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Api(value = "用户部分接口", tags = "用户部分接口", description = "用户接口")
@RestController
@RequestMapping("/v1/user")
public class UserUpdateController extends BaseController
{
    @Autowired
    private RedisKit redisKit;
    @Autowired
    private AliSMSConfiguration smsConfiguration;
    @Autowired
    private UserUpdateService userUpdateService;
    
    
    
    @ApiOperation(value = "用户-注册")
    @ApiImplicitParam(name = "dto", value = "注册实体", required = true, dataType = "RegisterDTO")
    @PostMapping(value = "/register", produces = "application/json")
    public String register(@RequestBody RegisterDTO dto)
    {
        //校验参数
        Map<String, String> checkMap = CheckNullParamKit.checkParam(dto);

        boolean result = MobileKit.checkMobile(dto.getPhone());
        if(!result)
        {
            checkMap.put("phone", "phone格式不正确.");
        }
        if(0 != checkMap.size())
        {
            return badParameter(checkMap);
        }
        //调用业务逻辑
        return userUpdateService.register(dto);
    }
    
    
    
    @ApiOperation(value = "用户-找回密码")
    @ApiImplicitParam(name = "dto", value = "找回密码实体类", required = true, dataType = "FindOutPasswordDTO")
    @PostMapping(value = "/findOutPassword", produces = "application/json")
    public String findOutPassword(@RequestBody FindOutPasswordDTO dto)
    {
        //校验参数
        Map<String, String> checkMap = CheckNullParamKit.checkParam(dto);
        if(0 != checkMap.size())
        {
            return badParameter(checkMap);
        }
        //调用业务逻辑
        return userUpdateService.findOutPassword(dto);
    }
    
    
    
    @ApiOperation(value = "用户-修改头像")
    @PostMapping(value = "/updateHeadImage", headers = "content-type=multipart/form-data", produces = "application/json")
    public String updateHeadImage(@RequestHeader String token, @RequestParam MultipartFile headImage)
    {
        //校验身份
        RedisUserInfo redisUserInfo = redisKit.checkUserToken(token);
        if(null == redisUserInfo)
        {
            return tokenBadRequest();
        }
        //调用业务逻辑
        return userUpdateService.updateHeadImage(redisUserInfo, headImage);
    }
    
    
    
    @ApiOperation(value = "保存个人信息")
    @ApiImplicitParam(name = "dto", value = "参数实体", required = true, dataType = "UpdateUserInfoDTO")
    @PostMapping(value = "/updateUserInfo", produces = "application/json")
    public String updateUserInfo(@RequestHeader String token, @RequestBody UpdateUserInfoDTO dto)
    {
        //校验参数
        Map<String, String> checkMap = CheckNullParamKit.checkParam(dto);
        if(0 != checkMap.size())
        {
            return badParameter(checkMap);
        }
        //校验身份
        RedisUserInfo redisUserInfo = redisKit.checkUserToken(token);
        if(null == redisUserInfo)
        {
            return tokenBadRequest();
        }
        //调用业务逻辑
        return userUpdateService.updateUserInfo(redisUserInfo, dto);
    }
    
    
    
    @ApiOperation(value = "绑定支付宝账号")
    @PostMapping(value = "/bindAlipayAccount", produces = "application/json")
    public String bindAlipayAccount()
    {
        
        
        
        
        
        return success();
    }
    
    
    
    @ApiOperation(value = "绑定微信账号")
    @PostMapping(value = "/bindWeChatAccount", produces = "application/json")
    public String bindWeChatAccount()
    {
        
        
        
        
        return success();
    }
    
    
    
    @ApiOperation(value = "绑定微博账号")
    @PostMapping(value = "/bindWeiBoAccount", produces = "application/json")
    public String bindWeiBoAccount()
    {
        
        
        
        
        return success();
    }
    
    
    @Autowired
    private MCUserBaseInfoRepository userBaseInfoRepository;
    @Autowired
    private MCUserBaseInfoImpRepository userBaseInfoImpRepository;
    @Autowired
    private MCUserInviteMappingRepository userInviteMappingRepository;
    
    
    @ApiOperation(value = "同步数据")
    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/syncUserInfo", produces = "application/json")
    public String syncUserInfo()
    {
        try
        {
            //查询旧的数据
            Date now = new Date();
            Iterator<MCUserBaseInfoImp> list = userBaseInfoImpRepository.findAll().iterator();
            while(list.hasNext())
            {
                MCUserBaseInfoImp imp = list.next();
                if(0 != imp.getOldFid())
                {
                    MCUserBaseInfoImp parentInfo = userBaseInfoImpRepository.findFirstByOldId(imp.getOldFid());
                    if(null != parentInfo && 0 < parentInfo.getId())
                    {
                        MCUserInviteMappingEntity ie = new MCUserInviteMappingEntity();
                        ie.setInviteUserId(parentInfo.getId());
                        ie.setUserId(imp.getId());
                        ie.setCreateDate(now);
                        userInviteMappingRepository.save(ie);
                    }
                }else
                {


                }
            }
        }catch(Exception ex)
        {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return success();
    }
    
    
    @ApiOperation(value = "用户签到(只能签当天)")
    @PostMapping(value = "/userSign", produces = "application/json")
    public String userSign(@RequestHeader String token)
    {
        //校验身份
        RedisUserInfo redisUserInfo = redisKit.checkUserToken(token);
        if(null == redisUserInfo)
        {
            return tokenBadRequest();
        }
        return userUpdateService.userSign(redisUserInfo);
    }
    
    
    
    
    
    
    
}